/**
 * Created by <a href="mailto:qq2325690622@gmail.com">邓超</a> on 2017/5/7.
 */
/// <reference path="../typings/globals/jquery/index.d.ts" />
var root = "http://baihechou.club/";
var api = root + "api/";
var articleDetail = api + "advert/article";
var avatarPrefix = root + "public/uploads/api/";
//noinspection JSUnusedGlobalSymbols
function onLoad(articleId) {
    $.post(articleDetail, {'article_id': articleId, 'user_id': 1}, //no side effect
        function (response) {
            console.log(response);
            var comments = response.data.articleComment; //从response中取出data中的文章评论
            var recentCommentsContainer = $('#recentCommentsContainer');
            if (comments.length > 3) {
                comments.slice(0, 3).forEach(function (it) {
                    recentCommentsContainer.append(getCommentItem(it));
                });
                var hiddenCommentsContainer_1 = $('#hiddenCommentsContainer');
                comments.slice(3, comments.length).forEach(function (it) {
                    hiddenCommentsContainer_1.append(getCommentItem(it));
                });
            }
            else {
                var moreComments = $('#moreComments');
                if (comments == null || comments.length == 0) {
                    moreComments.html("暂无评论").css("color", "#999999").attr("onclick", null);
                    return;
                }
                moreComments.hide();
                comments.forEach(function (it) {
                    recentCommentsContainer.append(getCommentItem(it));
                });
            }
        }, "json");
}
/**
 * 根据一条评论对象生成对应的元素
 * 应利用commentItem.html进行单元测试,以及预览
 */
function getCommentItem(comment) {
    var user = comment.user;
    if (user.username == null || user.username.length == 0) {
        user.username = "无名";
    }
    if (user.avatar == null || user.avatar.length == 0) {
        user.avatar = "../img/default_avatar.png";
    }
    else {
        user.avatar = avatarPrefix + user.avatar;
    }
    return "\n<table class=\"itemTable\">\n    <tr>\n        <td class=\"itemTd1\">\n            <img src=\"" + user.avatar + "\" class=\"itemUserAvatar\">\n        </td>\n        <td class=\"itemTd2\">\n            <div class=\"itemUserName\">" + user.username + "</div>\n            <div class=\"itemTimestamp\">" + getFormattedTimestamp(comment.timestamp) + "</div>\n        </td>\n    </tr>\n    <tr>\n        <td  class=\"itemTd1\"></td>\n        <td class=\"itemTd2\">\n            <div class=\"itemContent\">" + comment.content + "</div>\n        </td>\n    </tr>\n</table>";
}
/**
 * 格式化时间戳
 * @param timestamp 时间戳,s
 */
function getFormattedTimestamp(timestamp) {
    var date = new Date(Number(timestamp) * 1000);
    return date.getMonth() + 1 + "-" + date.getDate() + " " + date.getHours() + ":" + date.getMinutes();
}
//noinspection JSUnusedGlobalSymbols
/**
 * 当查看更多评论按钮按下时显示全部评论并隐藏按钮
 */
function showAllComments() {
    $('#hiddenCommentsContainer').slideDown();
    $('#moreComments').hide();
}
